// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
import UserData from '../UserData'
var resloader = require('../function/resLoader')
cc.Class({
    extends: cc.Component,

    properties: {
        skillLogoPrefab:{
            default:null,
            type:cc.Prefab

        },
    },
    start () {
        //获取组件
        this.UserData =UserData
        this.upgradeMultiplier = -1
        this.level = 1
        this.getComponent()
        this.upgradeManager.gainBaseNumber = 2

        this.updateDisplay()

        this.updateProgressBarWidth()
        this.UserData.runtimeState.buttonUpdateList.push([this.checkButtonStatus,this])
        this.UserData.runtimeState.chefStatusPrefabList.push(this)
        this.UserData.addCurrentMoney(0)
    },
    getComponent() {
        this.upgradeManager = this.node.getComponent('UpgradeManager')
        this.statusBar = this.node.getChildByName('Status Bar')

        this.levelLabel = this.statusBar.getChildByName('Middle Bar').getChildByName('Level Label')

        this.GPSLabel = this.statusBar.getChildByName('Middle Bar').getChildByName('GPS Label')
                
        this.button = this.node.getChildByName('升级按钮')
        this.GPSIncreaseLabel = this.button.getChildByName('GPS Increase Label')
        this.CostLabel = this.button.getChildByName('Cost Label')
        this.LevelIncreaseLabel = this.button.getChildByName('Level Increase Label')
        
        this.chefPhoto = this.node.getChildByName('Chef Photo')
        this.chefNameLabel = this.statusBar.getChildByName('Chef Name Label')
        this.skillBar = this.statusBar.getChildByName('Skill Bar')

        this.progressBar = this.chefPhoto.getChildByName('Upgrade Progress').getChildByName('bar')
        this.resloader = resloader
        this.NameLabel = this.statusBar.getChildByName('Chef Name Label')
        
    },
    loadChefData(chefData){
        if(!this.upgradeManager){
            this.getComponent()
        }
        this.chefData = chefData        
        this.upgradeManager.level= chefData.level
        this.upgradeManager.costBaseNumber = chefData.costBaseNumber
        this.upgradeManager.costMultiply = chefData.costMultiply
        this.upgradeManager.gainBaseNumber = chefData.gainBaseNumber
        this.upgradeManager.gainMultiply = chefData.gainMultiply
        this.upgradeManager.gainMultiplierList = []
        for(var i=0;i<chefData.gainMultiplierList.length;i++){
            this.upgradeManager.gainMultiplierList.push(cc.v2(chefData.gainMultiplierList[i].level,chefData.gainMultiplierList[i].times))
        }
        this.upgradeManager.gainMultiplierList = chefData.gainMultiplierList
        this.skillList = chefData.skillList
        this.skillLearnt = []
        this.skillMultiplyList = []
        // 载入头像
        this.resloader.localImgLoader("Chef/Avatar/" + this.chefData.basicData.chefID ,this.chefPhoto.getComponent(cc.Sprite))
        // 载入名称
        this.NameLabel.getComponent(cc.RichText).string = this.chefData.basicData.Name
        for(var i = 0;i<chefData.skillList.length;i++){
            let logo =null
            if(i>this.skillBar.children.length -1){
                logo = cc.instantiate(this.skillLogoPrefab)                
            }else{
                logo = this.skillBar.children[i]
            }            
            this.resloader.localImgLoader('Skill/' + this.chefData.skillList[i].skillID,logo.getComponent(cc.Sprite))
        }
        this.floor = this.chefData.floor
        
        

    },

    upgrade() { //按下button时触发。
        let upgradeResult = this.upgradeManager.calcUpgrade(this.upgradeMultiplier,this.UserData.getCurrentMoney())        
        if(upgradeResult[2]){
            this.upgradeManager.upgrade(upgradeResult[1])
            this.UserData.subCurrentMoney(upgradeResult[0])
            this.level = this.upgradeManager.level
            this.chefData.level = this.level
            
            this.updateDisplay()
        }
        this.updateProgressBarWidth()
        this.UserData.updateSkill(this.chefData)
        
        
    },

    updateProgressBarWidth(){
        let previousGainMultiplyLevel = 0
        for(var i=0;i<this.upgradeManager.gainMultiplierList.length;i++){
            if(this.level<this.upgradeManager.gainMultiplierList[i][0]){
                
                this.nextGainMultiplyLevel = this.upgradeManager.gainMultiplierList[i][0]
                this.nextGainMultiplyNumber = this.upgradeManager.gainMultiplierList[i][1]
                if(i!=0){
                    previousGainMultiplyLevel =this.upgradeManager.gainMultiplierList[i-1][0]
                }
                break
            }
        }
        if(i<this.upgradeManager.gainMultiplierList.length){
            this.progressBar.width = (this.level - previousGainMultiplyLevel)/(this.nextGainMultiplyLevel - previousGainMultiplyLevel) * (this.progressBar.parent.width -3)            
        }else{
            this.progressBar.width =  (this.progressBar.parent.width -3)
        }
        
    },
    updateDisplay(){
        this.level = this.upgradeManager.level
        this.levelLabel.getComponent(cc.RichText).string = "<color=#ffffff>Lv.</c><color=#00bcff>"+ this.level +"</color>"
        this.GPSLabel.getComponent('labelFixed').setNumber(this.upgradeManager.getCurrentGain(this.level), "<color=#777777>收入: $</c><color=#ffffff>"  ,"(?%)</color>")
    },
    checkButtonStatus(node) {        
        if(!node){
            node = this
        }
        let upgradeResult = node.upgradeManager.calcUpgrade(node.upgradeMultiplier,node.UserData.getCurrentMoney())
        node.button.getComponent(cc.Button).interactable = upgradeResult[2]
        let levelUpCostResult = node.upgradeManager.getLevelUpCost(node.upgradeMultiplier,node.UserData.getCurrentMoney())
        if( levelUpCostResult[0]===0 ){
            node.CostLabel.getComponent('labelFixed').setNumber(node.upgradeManager.getLevelUpCost(1,node.UserData.getCurrentMoney())[0],"升级 $","")
        }else{
            node.CostLabel.getComponent('labelFixed').setNumber(levelUpCostResult[0],"升级 $","")
        }
        node.GPSIncreaseLabel.getComponent('labelFixed').setNumber(node.upgradeManager.getLevelUpGain(upgradeResult[1]),"收入 +$","")
        if(upgradeResult[1] ===0){
            node.LevelIncreaseLabel.getComponent('labelFixed').string = "+ Lv." +  1
        }else{
            node.LevelIncreaseLabel.getComponent('labelFixed').string = "+ Lv." +  upgradeResult[1]
        }
        
    },

});
